import { Toast, Dialog } from 'qmp-component'
import { SimpleEvents } from '@/utils'
import { createSave, stopChange } from '@/apis/ctmCustomerChange/index'
/**
 * @params {object} businessData 经销商数据
 * @params {string} type 页面类型编码(数据字典编码：CTM_CUS_ARCHIVE_CHANGE_TYPE)
 * @params {proxy}
 * @params {function} callback 回调函数
 */
export function createCustomerChange(businessData: any, type: string, callback: any) {
  if (!businessData.code) {
    Toast('请选择经销商')
    return
  }
  const {
    businessTypeCode,
    businessTypeName,
    credentialNo,
    contactPhone,
    legalPerson,
    marketLevelName,
    personCode,
    personName,
    businessAgencyCode,
    businessAgencyName,
    officeTypeCode,
    officeTypeName,
    cooperateStart,
    cityLevelName
  } = businessData
  const params = {
    customerCode: businessData.code,
    customerName: businessData.name,
    credentialNo,
    businessTypeCode,
    businessTypeName,
    contactPhone,
    legalPerson,
    marketLevelName,
    personCode,
    personName,
    officeCode: businessAgencyCode,
    officeName: businessAgencyName,
    officeTypeCode,
    officeTypeName,
    cooperateStart,
    cityLevelName,
    type,
    schedule: 'A'
  }
  createSave(params).then((res: any) => {
    if (res && res.success === 1 && res.msg) {
      const id = res.msg
      SimpleEvents.emit('refreshCustomerChangeList')
      callback && callback(id)
    } else {
      Toast('新增操作失败')
    }
  })
}

export function stopCustomerChange(id: string, callback: any) {
  Dialog.confirm({
    message: '提交后将终止此变更数据，是否确认？'
  }).then(() => {
    stopChange(`${id}`).then((res: any) => {
      Toast('操作成功')
      SimpleEvents.emit('refreshCustomerChangeItem', id)
      callback && callback()
    })
  })
}
